package net.oschina.allchat.server.framework.core.commons.osgi.platform;

import java.net.URL;

import net.oschina.allchat.server.framework.core.commons.osgi.OsgiUtils;

import org.eclipse.osgi.service.datalocation.Location;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

public class Equinox implements IPlatform {
	public BundleContext bundleContext;
	
	public Equinox(BundleContext bundleContext) {
		this.bundleContext = bundleContext;
	}
	
	@Override
	public URL getHomeDirectory() {
		ServiceReference<Location> srLocation = OsgiUtils.getServiceReference(bundleContext,
				Location.class, "type", "osgi.configuration.area");
		
		Location location = null;
		if (srLocation != null) {
			location = bundleContext.getService(srLocation);
		}
		
		if (location != null) {
			return location.getURL();
		}
		
		return null;
	}

	@Override
	public URL getConfigurationDirectory() {
		ServiceReference<Location> srLocation = OsgiUtils.getServiceReference(bundleContext,
				Location.class, "type", "eclipse.home.location");
		
		Location location = null;
		if (srLocation != null) {
			location = bundleContext.getService(srLocation);
		}
		
		if (location != null) {
			return location.getURL();
		}
		
		return null;
	}
}
